<?php

namespace app\api\service;

use think\Controller;
use think\Db;
use Des3\DES3;

class Address extends Controller {

	// 获取收货地址列表
	public static function get_address_list($param) {
		try {
			$member_id = $param['member_id'];
			$data = db('address')->alias('a')
                ->field('a.*, p.region_name r_province, c.region_name r_city, e.region_name r_area')
                ->join('yybs_region p', 'p.region_id = a.province')
                ->join('yybs_region c', 'c.region_id = a.city')
                ->join('yybs_region e', 'e.region_id = a.area')
                ->where('member_id', $member_id)
                ->order('id desc')
                ->select();
			return api_return(200, $data);
		} catch (\Exception $e) {
			return api_return(0, [], $e->getMessage());
		}
	}

	// 编辑/添加 收货地址
	public static function save_address($param) {
		try {
			$id = isset($param['id']) ? $param['id'] : 0;
			$member_id = isset($param['member_id']) ? $param['member_id'] : 0;
			$data = [
			    'linkman' => $param['linkman'],
                'mobile' => $param['mobile'],
                'address' => $param['address'],
                'postcode' => $param['postcode']
            ];
            isset($param['province']) ? $data['province'] = $param['province'] : $a = 1;
            isset($param['city']) ? $data['city'] = $param['city'] : $a = 1;
            isset($param['area']) ? $data['area'] = $param['area'] : $a = 1;

			if (isset($param['is_default']) && $param['is_default'] == 1) {
                model('address')->save(['is_default' => 0], ['member_id' => $member_id]);
                $data['is_default'] = 1;
            }

            if ($id > 0) {
				model('address')->where('id', $id)->where('member_id', $member_id)->update($data);
			} else {
				$count = model('address')->where('member_id', $member_id)->count();
				if ($count >= 10) {
				    throw new \Exception('最多添加10个收获地址！');
				}
				$data['member_id'] = $member_id;
                model('address')->insert($data);
			}
			return api_return(200, []);
		} catch (\Exception $e) {
			return api_return(0, [], $e->getMessage());
		}
	}

	// 获取收货地址详情
	public static function get_address_info($param)
	{
		try
		{
			$id = isset($param['id']) ? $param['id'] : 0;
			$member_id = isset($param['member_id']) ? $param['member_id'] : 0;
			$data = model('address')->alias('a')
                ->field('a.*, p.region_name r_province, c.region_name r_city, e.region_name r_area')
                ->join('yybs_region p', 'p.region_id = a.province')
                ->join('yybs_region c', 'c.region_id = a.city')
                ->join('yybs_region e', 'e.region_id = a.area')
                ->where('id', $id)
                ->where('member_id', $member_id)->find();

			return api_return(200, $data);
		}
		catch (\Exception $e)
		{
			return api_return(0, [], $e->getMessage());
		}
	}

    // 获取收货地址详情
    public static function get_default_address_info($param)
    {
        try
        {
            $member_id = isset($param['member_id']) ? $param['member_id'] : 0;
            $data = model('address')->alias('a')
                ->field('a.*, p.region_name r_province, c.region_name r_city, e.region_name r_area')
                ->join('yybs_region p', 'p.region_id = a.province')
                ->join('yybs_region c', 'c.region_id = a.city')
                ->join('yybs_region e', 'e.region_id = a.area')
                ->where('is_default', 1)
                ->where('member_id', $member_id)->find();

            return api_return(200, $data);
        }
        catch (\Exception $e)
        {
            return api_return(0, [], $e->getMessage());
        }
    }

	// 删除收货地址
	public static function delete_address($param) {
		try {
			$id = isset($param['id']) ? $param['id'] : 0;
			$member_id = isset($param['member_id']) ? $param['member_id'] : 0;
			$address = model('address')->get(['id' => $id, 'member_id' => $member_id]);
			if (!$address)
			{
                throw new \Exception('收货地址不存在！');
			}
			if ($address['is_default'] == 1)
			{
                throw new \Exception('默认地址不能删除！');
			}
            model('address')->where('id', $id)->where('member_id', $member_id)->delete();
			return api_return(200, []);
		} catch (\Exception $e) {
			return api_return(0, [], $e->getMessage());
		}
	}

	// 设置默认收获地址
	public static function save_default_address($param)
	{
		// 启动事务
		Db::startTrans();
		try {
			$id = isset($param['id']) ? $param['id'] : 0;
			$member_id = isset($param['member_id']) ? $param['member_id'] : 0;
			model('address')->save(['is_default' => 0], ['member_id' => $member_id]);

            model('address')->save(['is_default' => 1], ['member_id' => $member_id, 'id' => $id]);
			Db::commit();
			return api_return(200, []);
		}
		catch (\Exception $e)
		{
			Db::rollback();
			return api_return(0, [], $e->getMessage());
		}
	}
}